/*
 * @Author: mikey.zhaopeng 
 * @Date: 2018-12-19 22:26:12 
 * @Last Modified by: mikey.zhaopeng
 * @Last Modified time: 2018-12-19 23:17:27
 */

import 'package:flutter/material.dart';
import './slider_indicator.dart' show SliderIndicator;

class HomeBanner extends StatefulWidget {
  final List banners;
  HomeBanner({Key key, this.banners}) : super(key: key);
  @override
  _HomeBannerState createState() => _HomeBannerState();
}

class _HomeBannerState extends State<HomeBanner> {
  int sliderIndex = 0;
  bool runing = false;
  PageController controller;

  @override
  void initState() {
    super.initState();
    controller = new PageController();
    run();
  }

  void run() async {
    this.runing = true;
    while (this.runing) {
      await Future.delayed(new Duration(seconds: 5));
      if (mounted) {
        setState(() {
          //设置下标
          sliderIndex =
              sliderIndex == widget.banners.length - 1 ? 0 : sliderIndex + 1;
          controller.animateToPage(this.sliderIndex,
              duration: new Duration(milliseconds: 600),
              curve: Curves.easeInOut);
        });
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: Alignment.bottomCenter,
      children: <Widget>[
        Card(
          margin: EdgeInsets.zero,
          elevation: 0,
          child: AspectRatio(
            aspectRatio: 9.0 / 5.0,
            child: PageView(
              controller: controller,
              onPageChanged: (index) {
                setState(() {
                  sliderIndex = index;
                });
              },
              children: widget.banners.map((ad)=>GestureDetector(
                 onTap: (){
                  //  final id = Uri.parse(ad['click']).queryParameters['id'];
                  //  Navigator.of(context).push(MaterialPageRoute(builder: (context)=>VideoPage(id:id,poster:ad['poster'])));
                 },
                 child: Image(
                   image: NetworkImage(ad['pic']),
                   fit: BoxFit.fill,
                 ),
              )).toList(),
            ),
          ),
        ),
        Padding(
          padding: const EdgeInsets.only(bottom: 8.0),
          child:  SliderIndicator(count: widget.banners.length,index: sliderIndex),
        )
      ],
    );
  }
}
